package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SwsSettingUpdAdImgController")
public class SwsSettingUpdAdImgController extends YxyApiBaseController {

    @Autowired
    private SwsSettingRepository swsSettingRepository;

    @ApiOperation("修改网站浮动广告")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/setting/adimg/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        String schoolId = getProfile(request, response).getSchoolId();

        String floatAdURL = reqModel.floatAdURL;
        String floatAdImg = reqModel.floatAdImg;
        boolean floatAdVisable = reqModel.floatAdVisable;

        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != swsSetting, "未获取到当前学校配置信息");

        swsSetting.setFloatAdURL(floatAdURL);
        swsSetting.setFloatAdImg(floatAdImg);
        swsSetting.setFloatAdVisable(floatAdVisable);
        swsSettingRepository.save(swsSetting);
        RespModel respModel = new RespModel();

        respModel.setMessage("修改成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String floatAdURL;
        public String floatAdImg;
        public boolean floatAdVisable;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(floatAdURL), "链接不能为空");
            boolean b1 = floatAdURL.startsWith("http://");
            boolean b2 = floatAdURL.startsWith("https://");
            Assert.isTrue(b1 || b2, "请以http://或https://开头");
            Assert.isTrue(StringUtils.isNotBlank(floatAdImg), "图片不能为空");
        }
    }

    protected class RespModel extends YxyApiRespModel {
        public RespModel() {
        }
    }
}
